using Gameplay.PVE.Config;
using Gameplay.PVE.Entity;
using Gameplay.PVE.Utils;
using UnityEngine;
using Yoozoo.Managers;

namespace Gameplay.PVE
{
    public class ShieldElement : ElementBase
    {

        private GameObject addEffect;
        private int addEffectId;
        private bool isBroken = false;
        public float shieldAmout;
        public override void Execute()
        {
            base.Execute();
            isBroken = false;
            AddShield();
            ShowAddEffect();
        }

        public override void Free()
        {
            base.Free();
            if (!isBroken)
            {
                RemoveShield();
                ShowFreeEffect();
            }
            else
            {
                ShowBrokenEffect();
            }
        }

        public override void Update()
        {
            base.Update();
            if (target.Data.TotalShield == 0 ||
                target.Data.TotalShield - target.Data.CurrentShield > shieldAmout)
            {
                isBroken = true;
                IsFree = true;
            }
        }

        private void ShowAddEffect()
        {
            int addEffectId = GetValue("AddEffect");
            var effect = PveResourceManager.GetResource(PveResourceManager.EPveResType.SkillEffect, addEffectId);
            if (effect != null)
            {
                effect.transform.SetParent(target.renderer.transform);
                effect.SetActive(true);
                effect.transform.localPosition = Vector3.zero;
                effect.transform.localEulerAngles = Vector3.zero;
                effect.transform.localScale = Vector3.one;
                this.addEffectId = addEffectId;
                this.addEffect = effect;
            }
        }

        private void RemoveAddEffect()
        {
            if (addEffect != null)
            {
                PveResourceManager.RecoverResource(addEffect,PveResourceManager.EPveResType.SkillEffect,addEffectId);
                addEffect = null;
            }
        }

        private void ShowFreeEffect()
        {
            RemoveAddEffect();
            int freeEffect = GetValue("FreeEffect");
            PveEffectManager.Instance.PlayEffect(freeEffect,target.Data.position,target.Data.forward,5,1,target.renderer.transform);
        }

        private void ShowBrokenEffect()
        {
            RemoveAddEffect();
            int effect = GetValue("DestroyEffect");
            PveEffectManager.Instance.PlayEffect(effect,target.Data.position,target.Data.forward,5,1,target.renderer.transform);
        }

        private void AddShield()
        {
            float value = PveOutputFunction.GetOutput(config.id, source.Data, target.Data,skill.level);
            shieldAmout = value;
            target.Data.CurrentShield += value;
            target.Data.TotalShield += value;
            target.Transmit(ETransmitType.UpdateHp);
        }

        private void RemoveShield()
        {
            target.Data.TotalShield -= shieldAmout;
            if (target.Data.CurrentShield > target.Data.TotalShield)
            {
                target.Data.CurrentShield = target.Data.TotalShield;
            }
            target.Transmit(ETransmitType.UpdateHp);
        }

    }
}